package com.example.demo.project.utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;

/**
 * Created by Administrator on 2017/9/21.
 */
public class FileUtils {
    public static String detectFileContentEncoding(Path filePath) throws IOException {
        try (InputStream is = new FileInputStream(filePath.toFile())) {
            try (BufferedInputStream bis = new BufferedInputStream(is)) {
                int p = (bis.read() << 8) + bis.read();
                switch (p) {
                    case 0xefbb:
                        return "UTF-8";
                    case 0xfffe:
                        return "Unicode";
                    case 0xfeff:
                        return "UTF-16BE";
                    default:
                        return "GBK";
                }
            }
        }
    }
}
